package com.bw.documents.local

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.bw.common.mvi.BaseViewModel
import com.bw.documents.local.entity.LocalDocumentsState
import com.bw.network.BaseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

class LocalViewModel : BaseViewModel<LocalRepo,LocalDocumentsState>() {
    override fun createRepo(): LocalRepo {
        return LocalRepo()
    }

    override fun createState(): LocalDocumentsState {
        return LocalDocumentsState(false,ArrayList())
    }

    //查询本地文件
    fun scanFiles(){
        viewModelScope.launch {
            repo.scanLocalDocuments()
                .catch { t : Throwable->
                    LogUtils.e("errorMsg:${t.message}") }
                .onStart { mutableContainer.updateRequestState { copy(request=true,complate = false) } }
                .onCompletion { mutableContainer.updateRequestState { copy(request=false,complate = true) } }
                .collect{
                    mutableContainer.updateUiState { copy(true,it) }
                }
        }
    }
}